import { useEffect } from 'react'
import { medicineRemind } from '@/services/medicine'
import { notification, Space, Button } from 'antd';
import { sort } from '@/utils/common'
export default () => {

    const handleDelay7Days = () => {
        const after7Days = new Date().getTime() + 7 * 24 * 60 * 60 * 1000
        localStorage.setItem('neverRemindTime', after7Days.toString())
        notification.destroy()
    }

    const remind = () => {
        medicineRemind().then(res => {
            if (res.code === 200 && res.dataList.length > 0) {
                const sortList = sort(res.dataList, 'daysDiff')
                const deadMedicine = sortList.filter(item => item.daysDiff < 0)  // 过期药品筛选
                const str = deadMedicine.map(item => item.name).join('、')
                const showMedicine = sortList.filter(item => item.daysDiff > 0)  // 未过期药品筛选
                const desc = <>
                    <div> <span style={{ color: 'red' }}>{str}</span>部分<span style={{ color: 'red', fontWeight: 'bolder' }}>已过期</span>，请尽快处理！</div>
                    {showMedicine.map((item, index) => {
                        return <div key={index}><span style={{ color: '#faad14' }}>{item.name}</span>部分即将过期，剩余<span style={{ color: '#faad14', fontWeight: 'bolder' }}>{item.daysDiff}</span>天</div>
                    })}
                </>
                notification.warning({
                    message: '过期药品提醒',
                    description: desc,
                    duration: null,
                    btn: <Space>
                        <Button type="link" size="small" onClick={handleDelay7Days}>
                            7天内不再提醒
                        </Button>
                        <Button type="link" size="small" onClick={() => notification.destroy()}>
                            确认
                        </Button>
                    </Space>
                });
            }
        })
    }
    useEffect(() => {
        const neverRemindTime = localStorage.getItem('neverRemindTime') || ''
        if (neverRemindTime !== '') {
            const now = new Date().getTime()
            // 判断是否过期
            if (now > parseInt(neverRemindTime)) {
                remind()
            }
        } else {
            remind()
        }
    }, [])
}